import HTTP from '@/utils/service/request';
// 通过 require 动态加载模块
// require 》代码运行时才加载，且是导入整个对象
// import 》代码编译时就加载，可按需导入
const modulesFiles = require.context('./modules', true, /\.js$/)
let modulesAPI = modulesFiles.keys().map(modulePath => modulesFiles(modulePath).default);

function mix(...mixins) {
  class API {
    constructor() {
      for (let mixin of mixins) {
        copyProperties(this, new mixin()); // 拷贝实例属性
      }
    }
  }

  for (let mixin of mixins) {
    copyProperties(API, mixin); // 拷贝静态属性
    copyProperties(API.prototype, mixin.prototype); // 拷贝原型属性
  }

  return new API();
}

function copyProperties(target, source) {
  for (let key of Reflect.ownKeys(source)) {
    if ( key !== 'constructor'
      && key !== 'prototype'
      && key !== 'name'
    ) {
      let desc = Object.getOwnPropertyDescriptor(source, key);
      Object.defineProperty(target, key, desc);
    }
  }
}

export default mix(HTTP, ...modulesAPI)
